A "good" object protocol, allows one to control meta, hence being able to modify the behaviour and the notation of the language itself. Python achieves this through dunder methods. Allowing python to be a multi-paradign language
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Providing an implementation of dunder len
and getitem
allows the class to be more explicit, in the sense it holds some data.
It is also worth mentioning a builtin's len method != dunder len, since directly reads the ob_size
field in python bytecode
Infix methods, such as ones those enable LHS and RHS representations, such as dunder le, gt etc should also be provided if is appropriate to do so
Muggle Methods: a feature not generally publicized, but makes
listcomps
and gencomps
are preferable over filter
and map
which largely are remnants of legacy python, it is almost always better to use explicit generators over such functionsarray.array
is a thin-wrapper around C's continuous memory aka Arrays. Great for storing homogeneous data, at low memory, since a list needs to allocate extra space to amortize the cost of a append()
call. match..case
should be used for matching the structure of an object, arbitiary conditions.ChainMap
merges all dictionaries to form a singular look-up-table. Updating this, auto-updates all the objects held in the table